﻿HmJS.$Import('core.element');

HmJS.register('base.element.rgba', function ($ns) {

	/*
	---
	version: 1.1-1
	description: Adding Element.Style the abiity to set and get rgba background values in a cross-browser fashion.
	license: MIT-style
	download: http://mootools.net/forge/p/element_rgba
	source: http://github.com/arieh/Element.rgba

	authors:
	- Arieh Glazer

	requires:
	- core/1.3: Core,Element

	provides: [Element.setRGBA,Browser.Features.rgba]

	...
	*/

	(function () {
		var re = /^rgba/,
				supported = null;

		if (has("css-enabled")) {
			try {
				el.style.color = "rgba(1,1,1,0.5)";
				supported = re.test(el.style.color);
				el.style.color = "";
			} catch (e) { }
		}

		Browser.Features.rgba = supported;

		var setrgba = function (r, g, b, a) {
			this.setStyle('background-color', 'rgba(' + arguments.join(',') + ')');
			return this;
		};

		var setrgbaie = function (r, g, b, a) {
			var rgba = Array.from(arguments);
			rgba[3] *= 100;

			for (var i = 0; i < 4; i++) {
				rgba[i] = rgba[i].toString(16);
				if (rgba[i].length == 1) rgba[i] = "0" + rgba[i];
			}

			var alpha = rgba[3];
			rgba.pop();
			rgba.unshift(alpha);

			this.style.backgroundColor = "transparent";
			this.style.zoom = 1;
			this.style.filter = "progid:DXImageTransform.Microsoft.gradient(startColorstr=#" + match.join("") + ",endColorstr=#" + match.join("") + ");";

			return this;
		};

		Element.implement({
			setRGBA: Browser.Features.rgba ? setrgba : setrgbaie
		});

	})();

	/*
	---
	version: 1.1-1
	description: Adding Element.Style the abiity to set and get rgba background values in a cross-browser fashion.
	license: MIT-style
	download: http://mootools.net/forge/p/element_rgba
	source: http://github.com/arieh/Element.rgba

	authors:
	- Arieh Glazer

	requires:
	- core/1.3: Core,Element
	- Element.rgba/1.0.1 : Element.setRGBA

	provides: [Element.setStyle,Element.עetStyle]
	...
	*/

	(function (o_setStyle, o_getStyle) {
		if (Browser.Features.rgba) { return; }

		var set_regex = /rgba\(([0-9a-fA-F]{1,3}),([0-9a-fA-F]{1,3}),([0-9a-fA-F]{1,3}),(.*)\)/;
		var get_regex = /progid:DXImageTransform\.Microsoft\.gradient\(startColorstr=#(.{2,2})(.{6,6})/;

		Element.implement({

			setStyle: function (property, value) {
				if (!property.test(/background/)) {
					o_setStyle.apply(this, arguments);
				}

				var match = value.match(set_regex);

				if (!match) {
					o_setStyle.apply(this, arguments);
					return;
				}

				match.shift();
				this.setRGBA.apply(this, match);

				return this;
			},

			getStyle: function (property) {
				if (!property.test(/background/)) {
					old_gset.apply(this, arguments);
				}

				var match = this.style.filter.match(get_regex);

				if (!match) {
					return o_getStyle.apply(this, arguments);
				}

				var rgb = match[2].hexToRgb(true);

				return 'rgba(' + rgb.join(',') + ',' + parseInt(match[1], 16) / 100 + ')';
			}

		});

	})(Element.prototype.setStyle, Element.prototype.getStyle);

});
